package ui.panels;

import entities.Patient;
import entities.PatientMaterial;
import entities.ViewPatientMaterial;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import manager.AdminBusiDriver;
import model.EditPatientMaterialModel;
import ui.windows.RemedySchedulerFrame;

public class PMaterialManagementPanel extends javax.swing.JPanel {

    /** Creates new form PMaterialManagementPanel */
    public PMaterialManagementPanel(RemedySchedulerFrame mainFrame) {
        initComponents();
        this.mainFrame = mainFrame;
        includeModel();
    }

    private void includeModel() {
        driver = (AdminBusiDriver) mainFrame.getDriver();        
        model = new EditPatientMaterialModel(driver.ShowInfoPatientMaterial());
        materialTable.setModel(model);
        materialTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        materialTable.setFillsViewportHeight(true);
        materialTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        DefaultTableCellRenderer cellRenderer1 = new DefaultTableCellRenderer();
        cellRenderer1.setBackground(Color.red);
        cellRenderer1.setForeground(Color.white);
        DefaultTableCellRenderer cellRenderer2 = new DefaultTableCellRenderer();
        cellRenderer2.setBackground(Color.blue);
        cellRenderer2.setForeground(Color.white);
        materialTable.getColumnModel().getColumn(0).setPreferredWidth(140);
        materialTable.getColumnModel().getColumn(1).setPreferredWidth(100);
        materialTable.getColumnModel().getColumn(2).setPreferredWidth(120);
        materialTable.getColumnModel().getColumn(2).setCellRenderer(cellRenderer1);
        materialTable.getColumnModel().getColumn(3).setPreferredWidth(120);
        materialTable.getColumnModel().getColumn(3).setCellRenderer(cellRenderer2);
        materialTable.getColumnModel().getColumn(4).setPreferredWidth(120);
        materialTable.getColumnModel().getColumn(5).setPreferredWidth(140);

        createMaterialPanel = new CreatePMaterialPanel(mainFrame, this);        
        target = 1;
    }

    private void swithAction() {
        int choise = cbActions.getSelectedIndex();
        switchTarget(choise);
        cbActions.setSelectedIndex(0);
    }

    private void switchTarget(int choise) {
        if (target != choise || target == 4) {
            switch (choise) {
                case 0:
                    break;
                case 1:
                    this.remove(viewControl);
                    txtPID.setEnabled(true);
                    viewControl = new JScrollPane(materialTable);
                    target = 1;
                    this.add(viewControl, BorderLayout.CENTER);
                    this.validate();
                    break;
                case 2:
                    this.remove(viewControl);
                    txtPID.setEnabled(false);
                    viewControl = new JScrollPane(createMaterialPanel);
                    target = 2;
                    this.add(viewControl, BorderLayout.CENTER);
                    this.validate();
                    break;
                case 3:
                    new Thread() {
                        @Override
                        public void run() {
                            deleteRecord();
                        }
                    }.start();
                    break;
            }
        }
    }

    public void showAllRecord() {
        switchTarget(1);
    }

    public void showCreateRecord() {
        switchTarget(2);
    }

    public boolean createRecord(PatientMaterial record) {
        boolean returnVal = driver.insertPatientMaterial(record);
        if (returnVal) {
            ViewPatientMaterial viewRecord = driver.showViewPMaterialByPK(record.getPatientID(), record.getEmployeeID(), record.getStartDate());
            model.addRecord(viewRecord);
        }
        return returnVal;
    }

    private void deleteRecord() {
        if (selectedRecord != null) {
            target = 4;
            int choise = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete this patient !", "Delete", JOptionPane.YES_NO_OPTION);
            if (choise == JOptionPane.YES_OPTION) {
                PatientMaterial anal = new PatientMaterial();
                anal.setPatientID(selectedRecord.getPatientID());
                anal.setEmployeeID(selectedRecord.getDoctorID());
                anal.setStartDate(selectedRecord.getStartDate());
                anal.setEndDate(selectedRecord.getEndDate());
                anal.setReason(selectedRecord.getReason());
                boolean returnVal = driver.deletePatientMaterial(anal);
                if (returnVal) {
                    model.removeRecord(selectedRecord);
                    JOptionPane.showMessageDialog(this, "Deleted");
                } else {
                    JOptionPane.showMessageDialog(this, "Cannot delete !", "Error", JOptionPane.WARNING_MESSAGE);
                }
            }
        } else {
            JOptionPane.showMessageDialog(null, "You migh select one patient !", "Message", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    public Patient getPatientByIDC(String IDC) {
        return driver.showPatientByIDC(IDC);
    }

    private void filter() {
        model.filter(txtPID.getText(), null);
    }

    private void setSelectedRecord() {
        int index = materialTable.getSelectedRow();
        if (index != -1) {
            selectedRecord = model.getAt(index);
        } else {
            selectedRecord = null;
        }
    }

    public void closeTarget() {
        switchTarget(1);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private RemedySchedulerFrame mainFrame;
    private AdminBusiDriver driver;
    private CreatePMaterialPanel createMaterialPanel;
    private EditPatientMaterialModel model;
    private ViewPatientMaterial selectedRecord;
    private int target;
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        cbActions = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        txtPID = new javax.swing.JTextField();
        viewControl = new javax.swing.JScrollPane();
        materialTable = new javax.swing.JTable();

        setBackground(new java.awt.Color(255, 255, 255));
        setLayout(new java.awt.BorderLayout());

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        cbActions.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Actions", "View", "Create", "Delete" }));
        cbActions.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbActionsActionPerformed(evt);
            }
        });

        jLabel1.setText("IdentityCard:");

        txtPID.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtPIDCaretUpdate(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtPID, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 241, Short.MAX_VALUE)
                .addComponent(cbActions, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbActions, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(txtPID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        add(jPanel1, java.awt.BorderLayout.PAGE_START);

        materialTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        materialTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                materialTableMouseClicked(evt);
            }
        });
        viewControl.setViewportView(materialTable);

        add(viewControl, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

private void cbActionsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbActionsActionPerformed
    swithAction();
}//GEN-LAST:event_cbActionsActionPerformed

private void txtPIDCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtPIDCaretUpdate
    filter();
}//GEN-LAST:event_txtPIDCaretUpdate

private void materialTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_materialTableMouseClicked
    setSelectedRecord();
}//GEN-LAST:event_materialTableMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbActions;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTable materialTable;
    private javax.swing.JTextField txtPID;
    private javax.swing.JScrollPane viewControl;
    // End of variables declaration//GEN-END:variables
}
